package com.axend.aerosense.room.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.room.ui.activity.RoomAddActivity;
import com.axend.aerosense.room.ui.adapter.RoomCreateRoomListAdapter;
import com.axend.aerosense.room.viewmodel.RoomTypeViewModel;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.app.BaseApplication;
import com.axend.aerosense.base.bean.RoomTypeBean;
import com.axend.aerosense.base.database.room.RoomTypeMananger;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.bean.RoomTypeInfoBean;
import com.axend.aerosense.common.services.ILoginService;
import com.axend.aerosense.common.services.config.ServicesConfig;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.room.R;
import com.axend.aerosense.room.databinding.RoomFragmentAddBinding;

import java.util.ArrayList;

/**
 * 显示房间类型,方便增加
 * @date :2020/12/30 13:38
 */

public class RoomCreateFragment extends MvvmBaseFragment<RoomFragmentAddBinding, BaseViewModel>
    implements ILoadDataResult<RoomTypeInfoBean> {
    private ArrayList<RoomTypeBean> roomList = null;
    private RoomCreateRoomListAdapter adapter = null;
    @Autowired(name = ServicesConfig.User.LONGING_SERVICE)
    ILoginService iLoginService;
    private long clickTime = 0;

    public static RoomCreateFragment getInstance(){
        RoomCreateFragment fragment = new RoomCreateFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(ARG_PARAM, str);
//        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.room_fragment_add;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected BaseViewModel getViewModel() {
        BaseViewModelFactroy<RoomTypeInfoBean> myViewModelFactroy = new BaseViewModelFactroy<RoomTypeInfoBean>(this, this);
        return new ViewModelProvider(this, myViewModelFactroy).get(RoomTypeViewModel.class);
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        ((RoomAddActivity)getActivity()).setTitle(getString(R.string.room_new_room));
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        setLoadSir(viewDataBinding.roomContentView);
        showLoading();
        viewModel.onLoad();
    }


    private void initView(){
        adapter = new RoomCreateRoomListAdapter();
        viewDataBinding.roomAddList.setAdapter(adapter);
        viewDataBinding.roomAddList.setLayoutManager(new GridLayoutManager(getContext(),3));
        adapter.setOnItemClickListener((adapter, view, position)->{
            long currentTime = System.currentTimeMillis();
            if(currentTime - clickTime < 1000){
                return;
            }
            clickTime = currentTime;
            RoomSettingFragment fragment = RoomSettingFragment.getInstance();
            Bundle bundle = new Bundle();
            bundle.putSerializable(RoomSettingFragment.OPEN_PARAM_ROOMTYPEINFO, roomList.get(position));
            fragment.setArguments(bundle);
            FragmentUtil.add(getActivity(), ((RoomAddActivity)getActivity()).getContentId(), fragment, this, true);

        });

//        viewDataBinding.roomAddList.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
//            @Override
//            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
//                switch (e.getAction()&e.ACTION_MASK){
//                    case MotionEvent.ACTION_DOWN:
//                        break;
//                    case MotionEvent.ACTION_POINTER_DOWN:
//                        return true;
//                }
//                return false;
//            }
//
//            @Override
//            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
//
//            }
//
//            @Override
//            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
//
//            }
//        });
    }

    @Override
    public void loadDataSuccess(RoomTypeInfoBean roomTypeInfoBean) {
        roomList = BaseApplication.getInstance().getRoomTypes();
        roomList.clear();
        roomList.addAll(roomTypeInfoBean.getRoomInfo());
        adapter.setList(roomList);
        RoomTypeMananger.getInstance(getActivity().getApplicationContext()).roomDao().insertList(roomList);

    }

    @Override
    public void loadDataFail(String decs) {
        showContent();
        roomList = BaseApplication.getInstance().getRoomTypes();
        if(null == roomList || roomList.size() == 0){
            roomList.addAll(RoomTypeMananger.getInstance(getActivity().getApplicationContext()).roomDao().getAll());
        }
        adapter.setList(roomList);
        if(null == roomList || roomList.size() == 0){
            ToastUtils.showShort(decs);
        }
    }
}
